package org.doubango.ngn.h264

import java.net.DatagramPacket
import java.net.DatagramSocket
import java.net.InetAddress

class H264Sender {
    private val mDatagramSocket: DatagramSocket = DatagramSocket()
    private val mDatagramPacket: DatagramPacket = DatagramPacket(ByteArray(0), 0, InetAddress.getByName("192.168.1.38"), 8888)
    @Suppress("PrivatePropertyName")
    private val _60K = 1024 * 60

    @Suppress("SpellCheckingInspection")
    fun sendH264Datas(datas: ByteArray) {
        val sendCount = datas.size / _60K
        val remainSize = datas.size % _60K
        if (sendCount > 0) {
            for (i in 0 until sendCount) {
                mDatagramPacket.setData(datas, i * _60K, _60K)
                mDatagramSocket.send(mDatagramPacket)
            }

            if (remainSize > 0) {
                mDatagramPacket.setData(datas, sendCount * _60K, remainSize)
                mDatagramSocket.send(mDatagramPacket)
            }
        } else {
            mDatagramPacket.data = datas
            mDatagramSocket.send(mDatagramPacket)
        }
    }

    fun close() {
        mDatagramSocket.close()
    }
}
